package ysq.xyg.sale.controller;

import org.springframework.ui.Model;
import ysq.xyg.base.aspect.BaseLog;
import ysq.xyg.base.util.BaseConstant;

import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.aspect.BaseLog;
import ysq.xyg.base.controller.BaseController;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.sale.model.po.SaleDetail;
import ysq.xyg.sale.model.po.SaleProduct;
import ysq.xyg.sale.model.vo.SaleDetailVo;
import ysq.xyg.sale.service.SaleDetailService;
import ysq.xyg.sale.service.SaleProductService;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.sys.service.SysUserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 销售模块-订单信息controller层
 *
 * @author shengQiang yu
 * @date 2019-9-9 14:28:03
 */
@RestController
public class SaleDetailController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(SaleDetailController.class);
    @Resource
    private SaleDetailService saleDetailService;
    @Resource
    private SaleProductService saleProductService;
    @Resource
    private SysUserService sysUserService;
    private SaleDetail saleDetail;

    @RequestMapping("/sale/*")
    public Model goPage(Model model) {
        return model;
    }
    /**
     * 我的订单分页查询
     *
     * @param saleDetailVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author shengQiang yu
     * @date 2019-9-9 14:28:03
     */
    @BaseLog(module = "销售模块",className = "订单信息",methods = "我的订单分页查询",businessType = BaseConstant.SYS_BUSINESS_TYPE_GET,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/saleDetail/myPage",method = RequestMethod.POST)
    public ReturnEntity myPage(HttpServletRequest request,SaleDetailVo saleDetailVo, int pageSize, int pageNum){
        saleDetailVo.setUserId(acceptCurrentUser(request).getId());
        ReturnEntity returnEntity = saleDetailService.page(saleDetailVo,pageSize,pageNum);
        return returnEntity;
    }
    /**
     * 分页查询
     *
     * @param saleDetailVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author shengQiang yu
     * @date 2019-9-9 14:28:03
     */
    @BaseLog(module = "销售模块",className = "订单信息",methods = "分页查询",businessType = BaseConstant.SYS_BUSINESS_TYPE_GET,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/saleDetail/page",method = RequestMethod.POST)
    public ReturnEntity page(SaleDetailVo saleDetailVo, int pageSize, int pageNum){
        ReturnEntity returnEntity = saleDetailService.page(saleDetailVo,pageSize,pageNum);
        return returnEntity;
    }

    /**
     * 跳转连接
     *
     * @param id 主键id
     * @author shengQiang yu
     * @date 2019-9-9 14:28:03
     */
    @RequestMapping(value = "/saleDetail/getEntity",method = RequestMethod.GET)
    public ModelAndView getEntity(String id){
        ModelAndView view = new ModelAndView("/sale/saleDetailUpdateOrSave");
        if(StrUtil.isNotEmpty(id)){
            saleDetail = saleDetailService.selectByPrimaryKey(id);
            if(saleDetail!=null){
                if(StrUtil.isNotEmpty(saleDetail.getProductId())){
                    SaleProduct saleProduct=saleProductService.selectByPrimaryKey(saleDetail.getProductId());
                    if(saleProduct!=null){
                        saleDetail.setProductName(saleProduct.getName());
                    }
                }
                if(StrUtil.isNotEmpty(saleDetail.getUserId())){
                    SysUser sysUser=sysUserService.selectByPrimaryKey(saleDetail.getUserId());
                    if(sysUser!=null){
                        saleDetail.setUserName(sysUser.getUserName());
                    }
                }
            }
            view.addObject("saleDetail", saleDetail);
        }
        return view;
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @author shengQiang yu
     * @date 2019-9-9 14:28:03
     */
    @BaseLog(module = "销售模块",className = "订单信息",methods = "删除",businessType = BaseConstant.SYS_BUSINESS_TYPE_DELETE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/saleDetail/delete")
    public ReturnEntity delete(HttpServletRequest request,String id){
        return saleDetailService.delete(id,acceptCurrentUser(request));
    }

    /**
     * 添加或修改
     *
     * @param saleDetailVo Vo对象
     * @author shengQiang yu
     * @date 2019-9-9 14:28:03
     */
    @BaseLog(module = "销售模块",className = "订单信息",methods = "添加或修改",businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/saleDetail/addOrUpdate",method = RequestMethod.POST)
    public ReturnEntity delete(HttpServletRequest request,SaleDetailVo saleDetailVo){
        return saleDetailService.addOrUpdate(saleDetailVo,acceptCurrentUser(request));
    }
}